<?php 
require_once ("DB.php");

/**
 * Update the content of demo feature
 * @param string $content
 * @return 
 */
function service_people_update($content) {
    $conn = getDBConnection();
    
    $conn->disconnect();
}

/**
 * Fetch the content of all seesions
 * @param $start where the result starts
 * @param $end where the result ends
 * @return array
 */
function service_people_get_peoplelist_by_catagory ($catagory, $start = 0, $end = 50) {
	$conn = getDBConnection();
	
	/** Fecht overview information*/
	$sql_stmt = "SELECT id_person, first_name, last_name, gender, mail, photo, title, enrolled_date
		FROM people
		WHERE catagory = '" . $catagory . "'
		ORDER BY enrolled_date ASC
		LIMIT ?, ?";
		
	$result = & $conn->query($sql_stmt, array($start, $end));
	
	$set = array();
	while ($row = &$result->fetchRow(DB_FETCHMODE_ASSOC)) {
        array_push($set, $row);
    }
    
    $result->free();
	
    $conn->disconnect();
    return $set;

}

/**
 * Fetch the content of all seesions
 * @param $id+person
 * @return array
 */
function service_people_get_person_by_id ($id_person) {
	$conn = getDBConnection();
	
	/** Fecht overview information*/
	$sql_stmt = "SELECT id_person, first_name, last_name, gender, mail, photo, title, enrolled_date
		FROM people
		WHERE id_person = $id_person
		LIMIT 1";

	$result = & $conn->query($sql_stmt);
	
	$person = array();
	while ($row = &$result->fetchRow(DB_FETCHMODE_ASSOC)) {
        $person['basic_info'] = $row;
		$person['optional_info'] = array();
    }
    
    $result->free();
	
	/* Fetch optional information*/
	
	$id_person = $person['basic_info']['id_person'];
	$sql_stmt = "SELECT profile_key, profile_value
		FROM people_profiles
		WHERE id_person = " . $id_person;

	$result = & $conn->query($sql_stmt);

	while ($row = &$result->fetchRow(DB_FETCHMODE_ASSOC)) {
		$person['optional_info'][$row['profile_key']] = $row['profile_value'];
	}
	$result -> free();

    $conn->disconnect();
    return $person;

}

function service_people_get_person_by_fullname ($full_name) {

	$sub_fullname = explode('_', $full_name);
	$first_name = ucwords($sub_fullname['0']);
	$last_name = ucwords($sub_fullname['1']);
	
	$conn = getDBConnection();
	$sql_stmt = "SELECT id_person, first_name, last_name, gender, mail, photo, title, enrolled_date
		FROM people
		WHERE first_name = '$first_name' AND last_name = '$last_name'
		LIMIT 1";
	
	$result = & $conn->query($sql_stmt);
	
	$person = array();
	while ($row = &$result->fetchRow(DB_FETCHMODE_ASSOC)) {
        $person['basic_info'] = $row;
		$person['optional_info'] = array();
    }
    
    $result->free();
	
	/* Fetch optional information*/
	
	$id_person = $person['basic_info']['id_person'];
	$sql_stmt = "SELECT profile_key, profile_value
		FROM people_profiles
		WHERE id_person = " . $id_person;

	$result = & $conn->query($sql_stmt);

	while ($row = &$result->fetchRow(DB_FETCHMODE_ASSOC)) {
		$person['optional_info'][$row['profile_key']] = $row['profile_value'];
	}
	$result -> free();

    $conn->disconnect();
    return $person;
}

/**
 * Edit a person's information
 *  @param $id_person
 *
 */
function service_people_edit_person ($id_person) {
	
}
?>